package com.edwin.ws.webservice.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXml {
	public static void main(String[] args) throws IOException, DocumentException {
		new ReadXml().getXml();
	}

	public boolean getXml() throws IOException, DocumentException {
		Document document = DocumentHelper.createDocument();
		Element root = DocumentHelper.createElement("soap:Envelope");
		root.addAttribute("xmlns:xsi",
				"http://www.w3.org/2001/XMLSchema-instance");
		root.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
		root.addAttribute("xmlns:soap",
				"http://schemas.xmlsoap.org/soap/envelope/");
		document.add(root);
		// 生成root的一个接点
		Element category = DocumentHelper.createElement("soap:Body");
		root.add(category);
		// 生产category的一个接点
		Element method = category.addElement("addService",
				"http://webservice.ws.edwin.com/");
		method.addElement("num1").addText("1");
		method.addElement("num2").addText("2");

		URL url = new URL("http://10.118.58.126:9098/ws/addServicePort?wsdl");
		URLConnection connection = url.openConnection();
		HttpURLConnection httpConn = (HttpURLConnection) connection;
		byte[] b = document.asXML().getBytes();
		// Set the appropriate HTTP parameters.
		httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		httpConn.setRequestProperty("SOAPAction",
				"");
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		OutputStream out = httpConn.getOutputStream();
		out.write(b);
		out.close();
		// Read the response and write it to standard out.
		InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
		BufferedReader in = new BufferedReader(isr);
		SAXReader reader = new SAXReader();
		Document resultDoc = reader.read(in);
		Element resultRoot = resultDoc.getRootElement();
		List rootList = resultRoot.selectNodes("/your xml node patht']");
		Element element = null;
		String resu = null;
		// 循环此节点,并取出对应的文本信息
		for (Object obj : rootList) {
			element = (Element) obj;
			resu = element.getTextTrim();
		}
		return "true".equals(resu);
	}
}
